﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Effects.Effects.GuiParts
{
    /// <summary>
    ///  Synchornizes <see cref="Footswitch"/> and <see cref="LED"/> to be turned on/off in the same time.
    /// </summary>
    public class FootswitchLedSynchronizer
    {
        #region fields
        // all fields and properties

        /// <summary>
        ///  Footswtich that turns LED on/off.
        /// </summary>
        private Footswitch footswitch;

        /// <summary>
        ///  Gets or sets the synchronized Fotswitch.
        /// </summary>
        public Footswitch Footswitch
        {
            get { return footswitch; }
            set
            {
                if (footswitch != null)
                    footswitch.Switched -= this.footswitchSwitched;
                footswitch = value;
                if (footswitch != null)
                    footswitch.Switched += this.footswitchSwitched;
            }
        }

        /// <summary>
        ///  <see cref="LED"/> to synchornize with footswitch;
        /// </summary>
        private LED led;

        /// <summary>
        ///  Gets or sets the synchornized LED.
        /// </summary>
        public LED Led
        {
            get { return led; }
            set { led = value; }
        }

        #endregion

        #region synchornization
        // synchornization of footswitch and LED

        /// <summary>
        ///  <see cref="E:FootswitchLedSynchronizer.footswitch.Switched"/> event handler. Turns LED on/off.
        /// </summary>
        /// <param name="sender"> Object that has raised the event == managed footswitch </param>
        /// <param name="args"> Event argumetns. </param>
        private void footswitchSwitched(object sender, FootswitchSwitchedEventArgs args)
        {
            led.On = args.On;
        }

        #endregion

        #region constructor
        // constructor

        /// <summary>
        ///  Creates new instance that synchronizes given footswitch and led.
        /// </summary>
        /// <param name="footswitch"> Footswitch that truns led on/off. </param>
        /// <param name="led"> Led synchornized with footswitch. </param>
        public FootswitchLedSynchronizer(Footswitch footswitch, LED led)
        {
            this.Footswitch = footswitch;
            this.Led = led;
        }

        #endregion

        #region set_value
        /// <summary>
        ///  According to given value turns On/Off both footswitch and LED. Fottswitch doesn't raise Switched event.
        /// </summary>
        /// <param name="on"> On/Off argument: true - on, false - off </param>
        public void SetValue(bool on)
        {
            footswitch.SetValue(on);
            led.On = on;
        }
        #endregion
    }
}
